#include <vector>
using std::vector;

#include <iostream>
using std::cin;

void Add()
{
	int n, m, t, r, c, va;
	cin >> n >> m >> t; // 输入

	vector<Element> bMatx;
	for (int i = 1; i <= t; i++)
	{
		cin >> r >> c >> va;
		Element a(r, c, va);
		bMatx.push_back(a);
	}


	if (Col != m || Row != n)
	{
		Row = n;
		Col = m;
		eleNum = t;
		ele = bMatx;
		cout << "                       ";
		cout << "-1" << endl;
		return;
	}

	
	vector<Element> nMatx;
	int aCount = 0, bCount = 0;
	int arank, brank;
	if (eleNum > 0)
		arank = (ele[0].row - 1) * Col + ele[0].col;
	if (t > 0)
		brank = (bMatx[0].row - 1) * Col + bMatx[0].col;
	while (aCount < eleNum && bCount < t)
	{
		if (aCount < eleNum && arank < brank)
		{
			nMatx.push_back(ele[aCount]);
			aCount++;
			if (aCount < eleNum)
				arank = (ele[aCount].row - 1) * Col + ele[aCount].col;
			continue;
		}
		if (bCount < t && brank < arank)
		{
			nMatx.push_back(bMatx[bCount]);
			bCount++;
			if (bCount < t)
				brank = (bMatx[bCount].row - 1) * Col + bMatx[bCount].col;
			continue;
		}
		if (brank = arank)
		{
			nMatx.push_back(Element(ele[aCount].row, ele[aCount].col, ele[aCount].value + bMatx[bCount].value));
			aCount++;
			bCount++;
			if (aCount < eleNum)
				arank = (ele[aCount].row - 1) * Col + ele[aCount].col;
			if (bCount < t)
				brank = (bMatx[bCount].row - 1) * Col + bMatx[bCount].col;
		}
	}

	if (aCount < eleNum)
		for (int i = aCount; i < eleNum; i++)
			nMatx.push_back(ele[i]);
	if (bCount < t)
		for (int i = bCount; i < t; i++)
			nMatx.push_back(bMatx[i]);
	eleNum = nMatx.size();
	ele = nMatx;
}
